package com.example.tys.oomdata.sqLiteData;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tys
 * @date 2018/3/9
 */

public class DaoSqlImpl<T> implements IBaseDao<T> {
    private final String tabName;

    public DaoSqlImpl(String tabName) {
        this.tabName = tabName;
    }

    @Override
    public long insert(T entry) {
        SQLiteDatabase db = DbHelper.getInstance().getWritableDatabase();
        ContentValues cv = DbHelper.obj2cv(entry);
        return insert(db, tabName, cv);
    }

    @Override
    public long insert(List<T> entries) {
        long ret = -1;
        SQLiteDatabase db = DbHelper.getInstance().getWritableDatabase();
        for (T entry : entries) {
            ContentValues cv = DbHelper.obj2cv(entry);
            ret = insert(db, tabName, cv);
        }
        return ret;
    }

    @Override
    public long delete(T entry) {
        SQLiteDatabase db = DbHelper.getInstance().getWritableDatabase();
        Condition where = DbHelper.uniqueObj(entry);
        if (null == where) {
            return 0;
        }
        Log.e("----", where.toString());
        return delete(db, tabName, where.whereClaus, where.whereArgs);
    }

    @Override
    public long update(T oldEntry, T entry) {
        SQLiteDatabase db = DbHelper.getInstance().getWritableDatabase();
        Condition where = DbHelper.uniqueObj(oldEntry);
        if (where == null) {
            return 0;
        }
        ContentValues cv = DbHelper.obj2cv(entry);
        return update(db, tabName, cv, where.whereClaus, where.whereArgs);
    }

    @Override
    public List<T> query(T where, String orderBy, Integer offset, Integer limit) {
        SQLiteDatabase db = DbHelper.getInstance().getReadableDatabase();
        Condition condition = DbHelper.queryWhere(where);
        if (null == condition) {
            condition = new Condition();
        }
        String limitArg = DbHelper.queryLimit(offset, limit);
        Cursor cursor = query(db, tabName, null, condition.whereClaus, condition.whereArgs,
                orderBy, limitArg);
        if (cursor.getColumnCount() <= 0) {
            cursor.close();
            return new ArrayList<>();
        }
        List<T> list = DbHelper.cursor2Obj(where.getClass(), DbHelper.parseFields(where.getClass()), cursor);
        cursor.close();
        return list;
    }


    private long insert(SQLiteDatabase sqLiteDatabase, String table, ContentValues cv) {
        //如果插入的所有字段null 抛出异常
        //nullColumnHack:所有字段为null是 向该字段写入数据。  INSERT INTO TYS(nullColumnHack) VALUES (NULL)
        return sqLiteDatabase.insert(table, null, cv);
    }

    private int delete(SQLiteDatabase sqLiteDatabase, String table, String whereClaus, String[] whereArgs) {
        return sqLiteDatabase.delete(table, whereClaus, whereArgs);
    }

    private int update(SQLiteDatabase sqLiteDatabase, String table, ContentValues cv, String whereClaus, String[] whereArgs) {
        return sqLiteDatabase.update(table, cv, whereClaus, whereArgs);
    }

    private Cursor query(SQLiteDatabase sqLiteDatabase, String table, String[] columns,
                         String selection, String[] selectionArgs, String orderBy, String limit) {
        return sqLiteDatabase.query(table, columns, selection, selectionArgs,
                null, null, orderBy, limit);
    }

    static class Condition {
        boolean and = true;
        String whereClaus;
        String[] whereArgs;

        public void setWhereClaus(String whereClaus) {
            this.whereClaus = whereClaus;
        }

        public void setWhereArgs(String[] whereArgs) {
            this.whereArgs = whereArgs;
        }

        public void setAnd(boolean and) {
            this.and = and;
        }
    }

}
